<extend name="./public/frame.html"/>
<block name="content">
    <style>
        .layui-form-label{width: 12%;}
        .layui-input-block{margin-left: 12%;}
    </style>
    <form class="layui-form layui-form-pane1" id="testForm" method="post" enctype="multipart/form-data">
        <table class="layui-table">
            <tr>
                <td style="text-align: center;width: 15%">导入手机号码</td>
                <td>
                    <button type="button" class="layui-btn uploads" >导入号码</button>
                    <a href="<?php echo $url;?>" style="color: green;margin-left: 20px;">电话号码模板 - 下载</a>
                </td>
            </tr>
            <tr>
                <td style="text-align: center;width: 15%">补发用户手机号</td>
                <td>
                    <table class="layui-table inside_price">
                        <tr>
                            <td>
                                <input type="text" name="phone[]" id="phone_01" value="" lay-verify="required" placeholder="用户手机号码" class="layui-input" style="width: 200px;float: left;" onkeyup="num(this)">
                                <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="look" style="width: 30px;float: left;margin-top: 8px;margin-left: 10px;" onclick="insidePrice()">+</a>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="text-align: center;">选择城市</td>
                <td>
                    <?php foreach($cityList as $key=>$value):?>
                    <input type="radio" name="region_id" title="{$value.region_name}" value="{$value.region_id}" style="margin-top: 5000px;!important;">
                    <?php endforeach;?>
                </td>
            </tr>


            <tr>
                <td colspan="3" style="padding-left: 100px;">
                    <input type="hidden" value="<?php echo $coupon_id;?>" name="coupon_id">
                    <input type="hidden" value="<?php echo $order_type;?>" name="order_type">
                    <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </td>
            </tr>
        </table>
    </form>
    <script>
        layui.use(['form','upload'], function () {
            form = layui.form,upload = layui.upload;
            form.on('submit(*)', function(data){
                var region_id = $("input[name='region_id']:checked").val();
                if(typeof(region_id) === 'undefined'){
                    layer.msg('请选择一个城市',{icon: 5});
                    return false;
                }
                $.ajax({
                    url:'{:url("/admin/AdminActivity/supplyCouponAdd")}',
                    async:true,
                    dataType:'json',
                    type:'POST',
                    data: $(data.form).serialize(),
                    success:function(res){
                        if(res.code==1){
                            layer.alert(res.msg, {icon: 6},function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                window.parent.location.reload()
                            });
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                return false;
            });
            upload.render({
                elem: '.uploads'//这个就等同于点击
                ,accept: 'file'
                ,url: '/admin/Upload/excelUpload'
                ,done:  function(res){ //上传后的回调
                    if(res.status == 1){
                        layer.msg('导入成功',{icon: 1});
                        var html = '';
                        var i  = $('.inside_price tr').length;
                        $.each(res.CHILD, function() {
                            i++;
                            if(i == 2){
                                $("#phone_01").val(this);
                            }else{
                                html += '<tr>\n' +
                                    '      <td>\n' +
                                    '          <input type="text" name="phone[]" id="phone_0'+i+'" value="'+this+'" placeholder="用户手机号码" class="layui-input" style="width: 200px;float: left;" onkeyup="num(this)">\n' +
                                    '          <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="look" style="width: 30px;margin-top: 8px;margin-left: 10px;" onclick="reduceInsidePrice(this)">-</a>\n' +
                                    '      </td>\n' +
                                '</tr>';
                            }
                        })
                        $(".inside_price").append(html);
                    }else{
                        layer.msg(res.msg);
                    }
                }
                ,error: function(e){
                    console.log(e)
                }
            });
        });
        //新增补发优惠券行数
        function insidePrice() {
            var i  = $('.inside_price tr').length;
            i++;
            var html = '<tr>\n' +
                '           <td>\n' +
                '                <input type="text" name="phone[]" id="phone_0'+i+'" value="" placeholder="用户手机号码" class="layui-input" style="width: 200px;float: left;" onkeyup="num(this)">\n' +
                '                <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="look" style="width: 30px;margin-top: 8px;margin-left: 10px;" onclick="reduceInsidePrice(this)">-</a>\n' +
                '           </td>\n' +
                '       </tr>';
            $(".inside_price").append(html);
        }
        //补发优惠券行数
        function reduceInsidePrice($this) {
            $($this).parent().parent().remove();
        }
        //验证只能输入正确的手机号码
        function num(object) {
            var value   = object.value;
            var id      = object.id;
            if (isNaN(value)){
                layer.msg('请输入正确的数字',{icon: 5});
                $("#"+id).val('');
                return false;
            }
        }
    </script>
</block>
